home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / 021-030 / amok24 / clusters / clusters.def next >
Text File  |  1993-11-04  |  5KB  |  107 lines

  1. (**********************************************************************
  2.  
  3.     :Program.    Clusters.def
  4.     :Contents.   block orientated memory management
  5.     :Author.     Nicolas Benezan [bne]
  6.     :Address.    Postwiesenstr. 2, D7000 Stuttgart 60
  7.     :Phone.      711/333679
  8.     :Copyright.  Public Domain
  9.     :Language.   Modula-2
  10.     :Translator. M2Amiga A+L V3.2d
  11.     :Imports.    BigSets, TaskMemory [bne]
  12.     :History.    V1.0 [bne] 02.Jul.1989
  13.     :History.    V1.1 [bne] 09.Jul.1989 (TYPEs optimized, bugs fixed)
  14.  
  15. **********************************************************************)
  16.  
  17. DEFINITION MODULE Clusters;
  18.  
  19. FROM BigSets    IMPORT BigSet;
  20. FROM SYSTEM     IMPORT ADDRESS;
  21.  
  22. TYPE
  23.   HeapPtr=POINTER TO Heap;
  24.   ClusterPtr=POINTER TO Cluster;
  25.   Heap=RECORD
  26.     clusterList: ClusterPtr;     (* sorted list, ascending address order *)
  27.     firstFreeCluster: ClusterPtr;(* Cluster with >=1 free blocks | NIL   *)
  28.     clusterSize: LONGINT;        (* total size of MemChunks for Clusters *)
  29.     blockSize: LONGINT;          (* size of blocks + 4 bytes overhead    *)
  30.     blocksPerCluster: CARDINAL;  (* maximum blocks per Cluster           *)
  31.   END;
  32.   Cluster=RECORD
  33.     next, pred: ClusterPtr;      (* pointers for double linked list      *)
  34.     heap: HeapPtr;               (* backward pointer to root of list     *)
  35.     blockAllocMap: BigSet;       (* bit set=block allocated, clear=free  *)
  36.     freeBlocks: CARDINAL;        (* number of remaining free blocks      *)
  37.     firstFreeBlock: CARDINAL;    (* undefined if freeBlocks=0            *)
  38.   (*data: ARRAY [0..blocksPerCluster-1],[0..blockSize+3] OF BYTE;        *)
  39.   END;
  40.   (* The following conditions must hold true before and after every      *)
  41.   (* access to a heap/cluster list:                                      *)
  42.   (* - Cluster.next is NIL or points to a Cluster at a higher address.   *)
  43.   (* - Cluster.pred points to a Cluster at a lower address or to         *)
  44.   (*   Heap.clusterList.                                                 *)
  45.   (* - firstFreeCluster points to the Cluster which has >=1 free blocks  *)
  46.   (*   at the lowest address.                                            *)
  47.   (* - There must never be a cluster which has all blocks free.          *)
  48.   (* - freeBlocks is equal to the number of free blocks in this Cluster. *)
  49.   (* - firstFreeBlock is the number of the free block at the lowest      *)
  50.   (*   address in this Cluster. If freeBlocks=0 then firstFreeBlock is   *)
  51.   (*   undefined.                                                        *)
  52.   (* - Each block of a Cluster has a coresponding bit in blockAllocMap.  *)
  53.   (*   A clear bit means block is free, a set one means block allocated. *)
  54.  
  55.   AllocationProc=PROCEDURE(VAR ADDRESS, LONGINT);
  56.   DeallocationProc=PROCEDURE(VAR ADDRESS);
  57.  
  58. VAR
  59.   HeapArray: HeapPtr; (* POINTER TO ARRAY [0..NumHeaps-1] OF Heap *)
  60.   NumHeaps: CARDINAL;
  61.  
  62. PROCEDURE InitMemManager(Allocation: AllocationProc;
  63.                          Deallocation: DeallocationProc;
  64.                          ClusterSize: LONGINT;
  65.                          BlockSizes: ARRAY OF LONGINT): BOOLEAN;
  66.  
  67. (*:Semantic. Startup procedure, must be called before any other operation.
  68.   :Input.    Allocation: procedure for global memory allocation
  69.   :Input.                (e.g. MemSystem.Allocate)
  70.   :Input.    Deallocation: procedure for global memory deallocation
  71.   :Input.                (e.g. MemSystem.Deallocate)
  72.   :Input.    ClusterSize: size of the blocks allocated from the global pool
  73.   :Input.    BlockSizes: array of the sizes of blocks which should be
  74.   :Input.                allocated by Allocate() in an optimized way
  75.   :Note.                 (the most time critical first... )
  76.   :Result.   TRUE = o.k., FALSE = startup failed
  77. *)
  78.  
  79. PROCEDURE Allocate(VAR Pointer: ADDRESS;
  80.                        Size: LONGINT);
  81.  
  82. (*:Semantic. Replaces the usual memory allocation procedures now. If a Size
  83.   :Semantic. is requested which is included in the BlockSizes array, the
  84.   :Semantic. allocation is handled with the help of cluster structures.
  85.   :Semantic. Otherwise a single block of memory is allocated.
  86.   :Semantic. If you allocate/deallocate a lot of small blocks of memory,
  87.   :Semantic. Clusters will improve performance and your free memory won't
  88.   :Semantic. be as scattered and hashed as it would be using AllocMem() or
  89.   :Semantic. similar procedures.
  90. *)
  91.  
  92. PROCEDURE Deallocate(VAR Pointer: ADDRESS);
  93.  
  94. (*:Semantic. Deallocates memory allocated by Allocate().
  95. *)
  96.  
  97. PROCEDURE FindBlock(    Block: ADDRESS;
  98.                     VAR ClPtr: ClusterPtr): CARDINAL;
  99.  
  100. (*:Semantic. Finds the Cluster to which the block belongs and returns the
  101.   :Semantic. number of the block within that cluster.
  102.   :Note.     for advanced aplications only (e.g. garbage collection)
  103. *)
  104.  
  105. END Clusters.
  106.  
  107.